﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Telerik.WinControls.UI;
using Telerik.WinControls.Data;

namespace Demo1
{
    public partial class Form1 : Form
    {
        DataTable banhang;
        public Form1()
        {
            InitializeComponent();
            banhang = GetSellProductsMapping();
            radGridView3.DataSource = banhang;
        }

        private void rbtn_nhapsanpham_Click(object sender, EventArgs e)
        {
            DataProvider provider = new DataProvider("QLBH");

            string[] abc = new string[8] ;

            abc[0] = rtxt_barcode.Text.Trim();
            abc[1] = rtxt_tenhang.Text.Trim();
            abc[2] = rtxt_macongty.Text.Trim();
            abc[3] = rtxt_maloaihang.Text.Trim();
            abc[4] = rtxt_soluong.Text.Trim();
            abc[5] = rtxt_donvitinh.Text.Trim();
            abc[6] = rtxt_giaban.Text.Trim();
            abc[7] = rtxt_gianhap.Text.Trim();

            int check_nhap = 2;
            check_nhap = provider.Insert(abc,check_nhap);
            if (check_nhap == 1)
            {
                MessageBox.Show("Them Du Lieu Thanh Cong");
            }
            else if (check_nhap == 2)
            {
                MessageBox.Show("That Bai");
            }
            else
            {
                MessageBox.Show("Ket noi that bai!");
            }

        }

        private void rtxt_timkiem_TextChanged(object sender, EventArgs e)
        {
            RadTextBox textbox = (RadTextBox)sender;
            string value = textbox.Text;
            if (string.IsNullOrEmpty(value))
            {
                radGridView1.FilterDescriptors.Clear();
            }
            FilterDescriptor filter = new FilterDescriptor();

            if (textbox.TextLength > 12)
            {
                filter.PropertyName = "mahang";
                filter.Operator = FilterOperator.Contains;
                filter.Value = value;
                filter.IsFilterEditor = true;
                if (radGridView1.FilterDescriptors.Count == 1) radGridView1.FilterDescriptors.Clear();
                radGridView1.FilterDescriptors.Add(filter);
                textbox.SelectAll();
            }
        }

        private void radPageView1_SelectedPageChanged(object sender, EventArgs e)
        {
            RadPageView page = (RadPageView)sender;

            if (page.SelectedPage.Item.Title.Equals("Thống Kê"))
            {
                DataProvider provider = new DataProvider("QLBH");
                DataTable da = provider.GetProducts();
                radGridView1.DataSource = da;
                radGridView1.MasterGridViewTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
                radGridView1.EnableFiltering = true;
            }
        }

        private void rbtn_capnhatsanpham_Click(object sender, EventArgs e)
        {
            DataProvider provider = new DataProvider("QLBH");

            string[] abc = new string[8];

            abc[0] = rtxt_barcode.Text.Trim();
            abc[1] = rtxt_tenhang.Text.Trim();
            abc[2] = rtxt_macongty.Text.Trim();
            abc[3] = rtxt_maloaihang.Text.Trim();
            abc[4] = rtxt_soluong.Text.Trim();
            abc[5] = rtxt_donvitinh.Text.Trim();
            abc[6] = rtxt_giaban.Text.Trim();
            abc[7] = rtxt_gianhap.Text.Trim();

            int check_capnhat = 5;
            check_capnhat = provider.Update(abc, check_capnhat);
            if (check_capnhat == 1)
            {
                MessageBox.Show("Cập Nhật Thanh Cong");
            }
            else if (check_capnhat == 2)
            {
                MessageBox.Show("Cập Nhật That Bai");
            }
            else
            {
                MessageBox.Show("Ket noi that bai!");
            }
        }

        private void rbtn_xoasanpham_Click(object sender, EventArgs e)
        {
            DataProvider provider = new DataProvider("QLBH");

            string barcode = rtxt_barcode.Text.Trim(),
                tenhang = rtxt_tenhang.Text.Trim();
            
            int check_xoa = 5;
            check_xoa = provider.Delected(barcode,tenhang, check_xoa);
            if (check_xoa == 1)
            {
                MessageBox.Show("Xóa Thanh Cong");
            }
            else if (check_xoa == 2)
            {
                MessageBox.Show("Xóa That Bai");
            }
            else
            {
                MessageBox.Show("Ket noi that bai!");
            }
        }
        private DataTable GetSellProductsMapping()
        {
            DataTable bh = new DataTable("BangGia");

            DataColumn dc1 = new DataColumn("STT");
            DataColumn dc2 = new DataColumn("Tên Sản Phẩm");
            DataColumn dc3 = new DataColumn("Đơn Vị");
            DataColumn dc4 = new DataColumn("Giá Bán");
            DataColumn dc5 = new DataColumn("Số Lượng");
            DataColumn dc6 = new DataColumn("Thành Tiền");
            
            bh.Columns.Add(dc1);
            bh.Columns.Add(dc2);
            bh.Columns.Add(dc3);
            bh.Columns.Add(dc4);
            bh.Columns.Add(dc5);
            bh.Columns.Add(dc6);
            return bh;
        }
        private void rtxt_barcode_banhang_TextChanged(object sender, EventArgs e)
        {
            string barcode = rtxt_barcode_banhang.Text.Trim();
            if (barcode.Length > 12)
            {
                DataProvider provider = new DataProvider("QLBH");


                DataRow row,row1;
                row = banhang.NewRow();
                row1 = provider.tk_barcode(barcode);

                row[0] = radGridView3.Rows.Count +1;//STT,TENSANPHAM,
                row[1] = row1["TENHANG"].ToString();//row1 :TENHANG,DONVITINH,GIABAN
                row[2] = row1["DONVITINH"].ToString();
                row[3] = row1["GIABAN"].ToString(); ;
                row[4] = "1";
                row[5] = (int.Parse(row[3].ToString()) * int.Parse(row[4].ToString())).ToString();
                
                banhang.Rows.Add(row);

                
                //radGridView3.MasterGridViewTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
                //radGridView3.EnableFiltering = true;
                
              
            }
        }

        private void rtxt_timkiem_banhang_TextChanged(object sender, EventArgs e)
        {
            string timkiem = rtxt_timkiem_banhang.Text;
            DataProvider provider = new DataProvider("QLBH");
            DataTable dt = provider.tk_tenhang(timkiem);
            radGridView2.MasterGridViewTemplate.AutoSizeColumnsMode = Telerik.WinControls.UI.GridViewAutoSizeColumnsMode.Fill;
            radGridView2.EnableFiltering = true;
            radGridView2.DataSource = dt;
            
        }

        private void radGridView2_DoubleClick(object sender, EventArgs e)
        {
            int select = radGridView2.SelectedRows.Count;
            if (select == 1)
            {
                GridViewRowInfo rowInfo;
                rowInfo = radGridView2.CurrentRow;
                string barcode = radGridView2.Rows[rowInfo.Index].Cells["MAHANG"].Value.ToString();
                
                DataProvider provider = new DataProvider("QLBH");

                DataRow row, row1;
                row = banhang.NewRow();
                row1 = provider.tk_barcode(barcode);

                row[0] = radGridView3.Rows.Count + 1;//STT,TENSANPHAM,
                row[1] = row1["TENHANG"].ToString();//row1 :TENHANG,DONVITINH,GIABAN
                row[2] = row1["DONVITINH"].ToString();
                row[3] = row1["GIABAN"].ToString(); ;
                row[4] = "1";
                row[5] = (int.Parse(row[3].ToString()) * int.Parse(row[4].ToString())).ToString();
                banhang.Rows.Add(row);
            }
        }

        private void radGridView3_CellValueChanged(object sender, GridViewCellEventArgs e)
        {
            string a="";
           // DataSet
        }
    }
}
